﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace OgrIsleri.DashBoard.Modul.Siniflar
{
    public partial class DersProgrami : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Request.QueryString["DeleteID"] != null)
                Sil();

            VeriDoldur();

            if (Request.QueryString["Guncelle"] != null)
                Guncelle();
        }
        private void Sil()
        {
            using (DbEntities db = new DbEntities())
            {
                int ID = Convert.ToInt32(Request.QueryString["DeleteID"]);
                var data = db.tblDersProgramis.Where(o => o.ID == ID).SingleOrDefault();
                db.tblDersProgramis.Remove(data);
                db.SaveChanges();
            }
            Response.Redirect("DersProgrami.aspx?SinifID=" + Request.QueryString["SinifID"]);
        }
        private void VeriDoldur()
        {
            using (DbEntities db = new DbEntities())
            {
                int SinifID=Convert.ToInt32(Request.QueryString["SinifID"]);
                var Donemler = db.tblDonemlers.OrderBy(o => o.DonemAdi).ToList();
                DpDonemler.DataSource = Donemler;
                DpDonemler.DataTextField = "DonemAdi";
                DpDonemler.DataValueField = "DonemID";
                DpDonemler.DataBind();


                var Liste = db.tblDersProgramis.Where(o => o.SinifID == SinifID).OrderBy(o => o.Gun).ToList();
                RptListe.DataSource = Liste;
                RptListe.DataBind();
            }
            for (int i = 0; i <= 23; i++)
            {
                DpSaat.Items.Add(new ListItem(){ Text=i.ToString("D2")+":00", Value=i.ToString("D2")+":00"});
                DpSaat.Items.Add(new ListItem() { Text = i.ToString("D2") + ":30", Value = i.ToString("D2") + ":30" });
            }

            
        }

        private void Guncelle()
        {
            using (DbEntities db = new DbEntities())
            {
                int ID = Convert.ToInt32(Request.QueryString["Guncelle"]);
                var data = db.tblDersProgramis.Where(o => o.ID == ID).SingleOrDefault();

                DpDonemler.ClearSelection();
                DpDonemler.Items.FindByValue(data.tblDersler.DonemID.ToString()).Selected = true;

                DersDoldur(Convert.ToInt32(DpDonemler.SelectedValue));

                DpDersler.ClearSelection();
                DpDersler.Items.FindByValue(data.DersID.ToString()).Selected = true;

                AkademisyenDoldur(Convert.ToInt32(data.DersID.ToString()));

                DpAkademisyenler.ClearSelection();
                DpAkademisyenler.Items.FindByValue(data.AkademisyenID.ToString()).Selected = true;

                DpGun.ClearSelection();
                DpGun.Items.FindByValue(data.Gun.ToString()).Selected = true;

                DpSaat.ClearSelection();
                DpSaat.Items.FindByValue(data.Saat).Selected = true;

            }
        }

        protected void BtnKaydet_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["Guncelle"] == null)
            {
                using (DbEntities db = new DbEntities())
                {
                    tblDersProgrami data = new tblDersProgrami();
                    data.DersID = Convert.ToInt32(DpDersler.SelectedValue);
                    data.AkademisyenID = Guid.Parse(DpAkademisyenler.SelectedValue);
                    data.Saat = DpSaat.SelectedValue;
                    data.SinifID = Convert.ToInt32(Request.QueryString["SinifID"]);
                    data.Gun = Convert.ToInt32(DpGun.SelectedValue);
                    db.tblDersProgramis.Add(data);
                    db.SaveChanges();
                }
            }
            else
            {
                using (DbEntities db = new DbEntities())
                {
                    int ID=Convert.ToInt32(Request.QueryString["Guncelle"]);
                    var data = db.tblDersProgramis.Where(o => o.ID == ID).SingleOrDefault();
                    data.DersID = Convert.ToInt32(DpDersler.SelectedValue);
                    data.AkademisyenID = Guid.Parse(DpAkademisyenler.SelectedValue);
                    data.Saat = DpSaat.SelectedValue;
                    data.Gun = Convert.ToInt32(DpGun.SelectedValue);
                    db.SaveChanges();
                }
            }
            Response.Redirect("DersProgrami.aspx?SinifID=" + Request.QueryString["SinifID"]);
        }
        
        protected void DpDonemler_SelectedIndexChanged(object sender, EventArgs e)
        {
            int DonemID = Convert.ToInt32(DpDonemler.SelectedValue);
            DersDoldur(DonemID);
        }

        protected void DpDersler_SelectedIndexChanged(object sender, EventArgs e)
        {
            int DersID = Convert.ToInt32(DpDersler.SelectedValue);
            AkademisyenDoldur(DersID);
        }

        private void DersDoldur(int DonemID)
        {
            using (DbEntities db = new DbEntities())
            {
                var Dersler = db.tblDerslers.Where(o => o.DonemID == DonemID).OrderBy(o => o.DersAdi).ToList();
                DpDersler.DataSource = Dersler;
                DpDersler.DataTextField = "DersAdi";
                DpDersler.DataValueField = "DersID";
                DpDersler.DataBind();
            }
        }

        private void AkademisyenDoldur(int DersID)
        {
            using (DbEntities db = new DbEntities())
            {
                var Akademisyenler = db.tblAkademisyenDetaylaris.Where(o => o.tblAkademisyenDersleris.Any(a => a.DersID == DersID)).OrderBy(o => o.AdSoyad).ToList();
                DpAkademisyenler.DataSource = Akademisyenler;
                DpAkademisyenler.DataTextField = "AdSoyad";
                DpAkademisyenler.DataValueField = "UserId";
                DpAkademisyenler.DataBind();
            }
        }
    }
}